/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.index.hash;

import java.io.IOException;

import org.omorphdb.core.index.structure.Pointer;
import org.omorphdb.core.io.Reader;
import org.omorphdb.core.io.Resource;

/**
 * Provides method to read on a provided resource elements like slots, buckets
 * and pointers, which should be used to manage a hash index.
 * 
 * @author Marco Tamburelli
 * 
 * @param <T>
 */
public abstract class HashReader<T>
{
	protected final Reader reader;

	HashReader(Resource resource) throws IOException
	{
		this.reader = resource.createReader();
	}

	/**
	 * Read the initial data sequence, which is the statistics descriptor of the
	 * index resource.
	 * 
	 * @return
	 * @throws IOException
	 */
	Statistics readStatistics() throws IOException
	{
		this.reader.setCursor(0);

		byte algorithm = this.reader.readByte();
		long filledSlotCount = this.reader.readLong();
		long collisionCount = this.reader.readLong();

		return new Statistics(algorithm, filledSlotCount, collisionCount);
	}

	/**
	 * Reads a pointer from the resource.
	 * 
	 * @param position
	 * @return
	 * @throws IOException
	 */
	Pointer readPointer(long position) throws IOException
	{
		this.reader.setCursor(position);

		return new Pointer(position, this.reader.readLong());
	}

	/**
	 * Reads a slot from the resource at the specified index from the slot
	 * buffer.
	 * 
	 * @param index
	 * @return
	 * @throws IOException
	 */
	Slot readSlot(long index) throws IOException
	{
		Slot slot = new Slot(index);
		this.reader.setCursor(slot.position());
		slot.target(this.reader.readLong());

		return slot;
	}

	/**
	 * Reads and returns the bucket pointed by a provided pointer.<br>
	 * 
	 * Warning: in case the pointer target is lesser than 0, or however an
	 * unidentified position, the behavior is unexpected since it's impossible
	 * to know how what it actually read could be interpreted. So the pointer
	 * should be checked before using it to search the bucket.
	 * 
	 * @param pointer
	 * @return
	 * @throws IOException
	 */
	Bucket<T> readBucket(Pointer pointer) throws IOException
	{
		long targetBucketPos = pointer.target();
		this.reader.setCursor(targetBucketPos);

		T value = readValue();
		long targetDocPosition = this.reader.readLong();

		long nextPointerPosition = this.reader.getCursor();
		long nextPointerTarget = this.reader.readLong();

		return new Bucket<T>(targetBucketPos, value, targetDocPosition, new Pointer(nextPointerPosition,
				nextPointerTarget));
	}

	protected abstract T readValue() throws IOException;
}
